//
//  SectionHeader.m
//  DownloadManagerDemo
//
//  Created by ejiang on 2021/12/10.
//

#import "SectionHeader.h"

@interface SectionHeader ()

@property (nonatomic, copy) ClickBlock block;

@property (nonatomic, copy) ChangeMaxNumBlock maxNumBlock;

@end

@implementation SectionHeader

- (instancetype)initWithCoder:(NSCoder *)coder {
//- (instancetype)init {
//    self = [super init];
    self = [super initWithCoder:coder];
    if (self) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blueColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(14);
            make.top.bottom.equalTo(self);
        }];
        
        _maxNumButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_maxNumButton setTitleColor:[UIColor linkColor] forState:UIControlStateNormal];
        _maxNumButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_maxNumButton setTitle:@"最多同时下载 1 个" forState:UIControlStateNormal];
        [_maxNumButton addTarget:self action:@selector(handleClickMaxNumButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_maxNumButton];
        [_maxNumButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.equalTo(_titleLabel.mas_right).offset(15);
            make.width.mas_equalTo(120);
        }];
        
        _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_actionButton setTitle:@"全部暂停" forState:UIControlStateNormal];
        [_actionButton setTitle:@"全部开始" forState:UIControlStateSelected];
        _actionButton.selected = YES;
        
        [_actionButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        _actionButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_actionButton addTarget:self action:@selector(handleClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_actionButton];
        
        [_actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(self);
            make.width.mas_equalTo(80);
        }];
    }
    return self;
}

- (void)configClickBlock:(ClickBlock)block {
    _block = [block copy];
}

- (void)configChangeMaxNumBlock:(ChangeMaxNumBlock)block {
    _maxNumBlock = [block copy];
}

- (void)handleClickAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (_block) {
        self.block(sender.isSelected);
    }
}

- (void)handleClickMaxNumButton:(UIButton *)sender {
    if (_maxNumBlock) {
        self.maxNumBlock();
    }
}

@end
